<?php
class Pageley_Domain_BasicFile implements Pageley_Domain_File {
    
    private $fileManager;
    private $location;
    private $uploadInfo;
    
    public function __construct($location) {
        $this->location = $location;
    }
    
    public function setFileManager($fileManager) {
        $this->fileManager = $fileManager;
    }
    
    public function getFileManager() {
        if($this->fileManager === null) {
            throw new Pageley_Domain_IllegalStateException("FileManager is not set.");
        }
        return $this->fileManager;
    }
    
    public function setUploadInfo($uploadInfo) {
        $this->uploadInfo = $uploadInfo;
    }
    
    public function getUploadInfo() {
        return $this->uploadInfo;
    }
    
    public function setLocation($location) {
        return $this->location = $location;
    }
    
    public function getLocation() {
        return $this->location;
    }
    
    public function getContextLocation() {
        return $this->getFileManager()->getContextPath() . $this->location;
    }
    
    public function getFile() {
        return new Pageley_Util_File($this->getFileManager()->getDirectory() . $this->location);
    }
    
    public function getMimeType() {
        return $this->getFileManager()->deriveMimeType($this->getExtension());
    }
    
    public function getExtension() {
        $extension = "";
        if(Pageley_Util_String::contains($this->location, ".")) {
            $extension = $this->location;
            while(Pageley_Util_String::contains($extension, ".")) {
                $extension = Pageley_Util_String::substringAfter($extension, ".");
            }
        }
        return $extension;
    }
    
    public function __toString() {
        return $this->location;
    }
}